// 菜单栏，开始/暂停
export class MenuPanel {
    private declare scene: Phaser.Scene;
    private declare control: Phaser.GameObjects.Sprite;
    private declare bgm: Phaser.Sound.NoAudioSound | Phaser.Sound.HTML5AudioSound | Phaser.Sound.WebAudioSound;

    constructor(scene: Phaser.Scene, sceneType: number) {
        this.scene = scene;
        const width = this.scene.cameras.main.width;
        this.control = this.scene.add.sprite(width - 150, 150, 'ImgPause')
            .setOrigin(0, 0)
            .setInteractive();
        const music = sceneType == 1 ? 'AudioEstimate' : sceneType == 2 ? 'AudioMultidimensional' : 'AudioCognition'
        this.bgm = this.scene.sound.add(music, {loop: true});
        this.bgm.play();
        this.addEvent();
    }

    resumeMusic(hide: boolean) {
        if (hide) {
            this.bgm.pause();
            this.scene.sys.game.pause();
        } else {
            this.bgm.resume();
            this.scene.sys.game.resume();
        }
    }

    private addEvent(): void {
        this.control.on("pointerdown", () => {
            if (this.scene.sys.game.isPaused) return;
            this.control.setTexture('ImgStart');
            this.scene.time.delayedCall(200, () => {
                this.bgm.pause();
                this.scene.sys.game.pause();
            });
        }, this);
        // 暂停后使用全局input判断点击的位置是否是暂停/开始钮
        this.scene.input.on('pointerdown', (event: any) => {
            if (this.scene.sys.game.isPaused) {
                const scale: number = 0.7;
                if (event.x > this.control.x && event.x < this.control.x + this.control.width * scale) {
                    if (event.y > this.control.y && event.y < this.control.y + this.control.height * scale) {
                        this.bgm.resume();
                        this.scene.sys.game.resume();
                        this.control.setTexture('ImgPause');
                    }
                }
            }
        })
    }
}